package com.itbour.service.tg.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.util.StringUtils;
import com.itbour.dao.tg.NotifyInfoDao;
import com.itbour.dto.tg.NotifyInfoDto;
import com.itbour.service.tg.NotifyInfoService;

public class NotifyInfoServiceImpl implements NotifyInfoService{
	private NotifyInfoDao notifyInfoDao;
	@Override
	public Page pageQueryStats(Map<String, Object> param, Integer pageNo,
			Integer pageSize) {
		Page result = null;
		
		// 实际起始位置
		int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
		int totalSize = notifyInfoDao.notifyInfoCnt(param);
		
		while(totalSize >0 && totalSize <= skipResults){
			skipResults= skipResults - pageSize;
			pageNo = pageNo -1;
		}
		
		// 分页检索
		param.put("pageSize", pageSize);
		param.put("skipResults", skipResults);
		List<NotifyInfoDto> data = notifyInfoDao.notifyInfo(param);

		result = new JspPage(skipResults, totalSize, pageSize, data);
		return result;
	}
	
	@Override
	public List<NotifyInfoDto> selectNotifyInfo(Map<String, Object> param) {
		return notifyInfoDao.selectNotifyInfo(param);
	}
	
	@Override
	public void delTopic(String ids) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(ids)));

		//param.put("ids",ids);//可包含多个
		notifyInfoDao.deleteNotifyInfoLogic(param);
		//notifyInfoDao.deleteTopicRInfoLogic(param);// 一并删除关联信息
	
		
	}
	
	@Override
	public void updNotify(Map<String, Object> param) {//更新
		notifyInfoDao.updateNotifyInfoById(param);
	}
	
	@Override
	//新增
	public void addNotify(NotifyInfoDto notifyInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("type", notifyInfo.getType());
		param.put("title", notifyInfo.getTitle());
		param.put("message", notifyInfo.getMessage());
		param.put("image", notifyInfo.getImage());
		param.put("url",  notifyInfo.getUrl());
		param.put("encrypt",  notifyInfo.getEncrypt());
		param.put("start_time", notifyInfo.getStart_time());
		param.put("end_time", notifyInfo.getEnd_time());
		param.put("chk_freq", notifyInfo.getChk_freq());
		param.put("update_by", notifyInfo.getUpdate_by());
		
		param.put("display_type", notifyInfo.getDisplay_type());
		param.put("embed_url", notifyInfo.getEmbed_url());
		if (notifyInfo.getEmbedW()==null){
			param.put("w", 0);// 宽
		} else {
			param.put("w", notifyInfo.getEmbedW());// 宽
		}
		
		if (notifyInfo.getEmbedH() == null){
			param.put("h", 0);// 高
		} else {
			param.put("h", notifyInfo.getEmbedH());// 高
		}
		
		notifyInfoDao.insertNotifyInfo(param);
	}
	
	@Override
	public Page pageQueryNotifyEditMsg(Map<String, Object> param,
			Integer pageNo, Integer pageSize) {
		Page result = null;
		
		// 实际起始位置
		int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
		int totalSize = notifyInfoDao.notifyEditMsgCnt(param);
		
		while(totalSize >0 && totalSize <= skipResults){
			skipResults= skipResults - pageSize;
			pageNo = pageNo -1;
		}
		
		// 分页检索
		param.put("pageSize", pageSize);
		param.put("skipResults", skipResults);
		List<NotifyInfoDto> data = notifyInfoDao.notifyEditMsgInfo(param);

		result = new JspPage(skipResults, totalSize, pageSize, data);
		return result;
	}
	
	@Override
	public Page pageQueryNotifyUserMsg(Map<String, Object> param,
			Integer pageNo, Integer pageSize) {
		Page result = null;
		
		// 实际起始位置
		int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
		int totalSize = notifyInfoDao.notifyEditUserCnt(param);
		
		while(totalSize >0 && totalSize <= skipResults){
			skipResults= skipResults - pageSize;
			pageNo = pageNo -1;
		}
		
		// 分页检索
		param.put("pageSize", pageSize);
		param.put("skipResults", skipResults);
		List<NotifyInfoDto> data = notifyInfoDao.notifyEditUserInfo(param);

		result = new JspPage(skipResults, totalSize, pageSize, data);
		return result;
	}
	
	@Override
	public void addNotifyMsg(NotifyInfoDto notifyInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("notify_id", notifyInfo.getNotify_id());
		param.put("user_id", notifyInfo.getUser_id());
		param.put("update_by", notifyInfo.getUpdate_by());
		notifyInfoDao.insertNotifyMsg(param);
	}
	
	public NotifyInfoDao getNotifyInfoDao() {
		return notifyInfoDao;
	}
	public void setNotifyInfoDao(NotifyInfoDao notifyInfoDao) {
		this.notifyInfoDao = notifyInfoDao;
	}

}
